package serialize;

import java.io.*;

public class serializedemo {
    public static void main(String[] args) {
        User user =new User("cg",22,"男");

        try{
            FileOutputStream fileOut=new FileOutputStream("user.ser");
            ObjectOutputStream out =new ObjectOutputStream(fileOut);
            out.writeObject(user);
            out.close();
            fileOut.close();
            System.out.println("已序列化");


        } catch (IOException i){
            i.printStackTrace();
        }

        try{
            FileInputStream fileIn= new FileInputStream("user.ser");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            User deserializedUser =(User) in.readObject();
            in.close();
            fileIn.close();

            System.out.println("反序列化User：");
            System.out.println(deserializedUser);
        }catch (IOException i){
            i.printStackTrace();
        }catch (ClassNotFoundException c){
            System.out.println("没有user类");
            c.printStackTrace();
        }
    }

}
